// pages/orderSheet/applyAfterSale.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: '',
    imgPath: "/images",

    //订单号
    order_sn: '',
    //详情
    detail: null,
    //退款类型
    refundType: '退款',
    //可选择的退款类型
    refundTypes: ["退款", "换货"],
    //退款金额
    price: '',
    //申请理由
    reason: '',
    //上传凭证
    images: [''],

    //是否显示选择类型弹窗
    showSelType: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    // console.log("options", options)

    const util = require("../../utils/util")
    this.setData({
      imgPath: util.getImgPath()
    })

    //订单号
    this.setData({
      order_sn: options.order_sn,
    })

    //清空图片列表
    this.setData({
      images: []
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  //取得订单详情
  let that = this;
  const api = require("../../api/api")
  api.user.orderDetail({
    order_sn: this.data.order_sn,
    success(res) {
      // console.log("orderDetail,res", res)

      //保存缓存
      wx.setStorageSync('orderDetail', res.data.data);

      //保存详情
      that.setData({
        detail: res.data.data,
        price: res.data.data.price,
      })
    }
  })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTabbarChange(event) {
    // console.log("onTabbarChange,event", event)
    this.setData({
      active: event.detail
    })
  },
  /**
   * 选择类型
   */
  selType() {
    this.setData({
      showSelType: true
    })
  },
  onSelTypeClose() {
    this.setData({
      showSelType: false
    })
  },
  /**
   * 选中类型
   * @param {*} e 
   */
  onSelTypeChange(e) {
    // console.log("onSelTypeChange,e", e)
    this.setData({
      refundType: e.detail.value,
    })

    // console.log("this.data", this.data)
  },
  /**
   * 提交申请
   */
  apply() {
    const that = this;

    if (this.data.refundType == "") {
      wx.showToast({
        title: '请选择退款类型',
        icon: 'none',
      })
      return;
    }

    if (this.data.reason == "") {
      wx.showToast({
        title: '请输入申请理由',
        icon: 'none',
      })
      return;
    }

    //申请售后
    const api = require("../../api/api")
    api.afterSale.apply({
      order_sn: this.data.order_sn,
      content: this.data.reason,
      // images: JSON.stringify(this.data.images),
      images: this.data.images,
      type: this.data.refundType=="退款"?"1":"2",
      success(res) {
        // console.log("afterSale.apply,res", res);

        if (res.data.code == 0) {
          wx.showToast({
            title: '申请售后成功！',
          });
          wx.navigateTo({
            url: '/pages/orderSheet/myOrderSheet',
          })
        } else {
          wx.showToast({
            title: res.data.msg,
          })
        }

      }
    })
  },
  /**
   * 处理七牛上传
   */
  addImage: function () {
    var that = this;

    if(this.data.images.length>=3){
      wx.showToast({
        title: '最多上传3张图片',
        icon:'none',
      })
    }

    const util = require("../../utils/util")

    //七牛库
    const qiniuUploader = require("../../utils/qiniuUploader");

    //选择图片
    wx.chooseImage({
      count: 1,
      success: function (res) {
        var filePath = res.tempFilePaths[0];

        const api = require("../../api/api");
        //取得上传token
        api.user.getUploadToken({
          success(res) {
            // console.log("getUploadToken,res", res);

            //token
            let token = res.data.data;

            //上传url
            // let uploadURL=util.getQiNiuUrl()+"/upload/"+dtStr;
            let uploadURL=util.getQiNiuUrl();

            //date
            let dt=new Date();
            let dtStr=""+dt.getFullYear()+util.formatNumber(dt.getMonth()+1)+util.formatNumber(dt.getDate())

            //filename
            let index=filePath.lastIndexOf("/")
            let filename=filePath.substr(index);

            //key
            let key="upload/"+dtStr+filename;

            // console.log("uploadURL",uploadURL,"key",key)

            //调用七牛上传
            qiniuUploader.upload(filePath,
              function (res) {
                // console.log("qiniuUploader,res", res)
                that.data.images.push(uploadURL+res.imageURL);
                that.setData({
                  images: that.data.images,
                });
                // console.log("that.data",that.data)
              },
              function (error) {
                // console.log('qiniuUploader,error: ' + error);
              }, {
                region: 'SCN',
                uploadURL: uploadURL,
                // domain: util.getQiNuiBucket,
                uptoken: token,
                // uptokenURL: 'UpTokenURL.com/uptoken',
                qiniuShouldUseQiniuFileName:false,
                key:key,
              });
          }
        })



      }

    })
  }
})